from os import name
import sqlite3

conn = sqlite3.connect('./db/src/set.db')
c = conn.cursor()


def assemble_conditions(keys, vals):
    # 组装条件语句
    msgs = []
    for a in range(len(vals)):
        if vals[a] != '':
            msgs.append([keys[a], vals[a]])
    msg =''
    if len(msgs)>=2:
        msg = msg + ' where '
        for k in msgs:
            msg = str(msg)+str(k[0]) + str(' like "%')+'%s'%k[1]+str('%" and ')
        msg = msg[:-5]
    elif len(msgs)==1:
        msg = msg + ' where '+ msgs[0][0] + ' like "%'+'%s'% msgs[0][1]+'%"'
    return msg


class selectDb():
    def __init__(self):
        self.conn = sqlite3.connect('test.db')
        self.c = conn.cursor()
    
    def select_users(self,id='',loginname='',username=''):
        # 更具条件组合查询用户信息，不包含密码
        keys= ['id', 'loginname', 'username']
        vals = [id, loginname, username]
        msg = assemble_conditions(keys, vals)
        sql = 'select id,loginname,username,update_time from user' + msg + ";"
        cursor = self.c.execute(sql)
        text = []
        for row in cursor:
            text.append(row)
        return text
    
    def judge_name_pass(self,loginname,password):
        # 判断帐号密码是否一致
        sql = 'select count(*) from user where loginname="%s" and password="%s";'% (loginname,password)
        cursor = self.c.execute(sql)
        for row in cursor:
            if row[0]==0:
                return False
            return True

    def close_db(self):
        # 断开连接
        self.conn.close()



        
        

if __name__=="__main__":
    print(selectDb().select_users(loginname='admin'))
    selectDb().close_db()
